Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Runtime.InteropServices
Imports System.Configuration
Imports System.Text
Public Class AdminMerchant
    'Private oAudit As viaAudit.AuditEvent
    Private x As String
    Public SESSION_USER_ID As String
    Public SESSION_VIAKLIX_ID As String
    Public SESSION_COMPANY_ID As Integer
    Public nPage As Long
    Public nNumPages As Long
    Public nPageRecords As Integer
    Public PassWord As String
    Public CompanyID As Integer
    Public CompanyName As String
    Public AccountName As String
    Public CompanyAlias As String
    Public Address1 As String
    Public Address2 As String
    Public City As String
    Public State As String
    Public ZipCode As String
    Public Contact_Name As String
    Public Contact_Phone1 As String
    Public Contact_Phone2 As String
    Public Contact_Email As String
    Public MerchantURL As String
    Public MerchantEmail As String
    Public TerminalEmail As String
    Public TerminalNumber As String
    Public sPin As String
    Public Active As String
    Public rapprvlmethod As String
    Public rapprvlgetURL As String
    Public rapprvlPostURL As String
    Public rapprvlText As String
    Public rapprvlNotifyMerchant As String
    Public rapprvlNotifyCustomer As String
    Public rdeclMethod As String
    Public rdeclGetURL As String
    Public rdeclPostURL As String
    Public rdeclText As String
    Public rdeclNotifyMerchant As String
    Public rdeclNotifyCustomer As String
    Public autoPend As String
    Public autoSettle As String
    Public autoSettleTime As String
    Public dateLastAutoSettle As Date
    Public settleInProgress As String
    Public integratePurchaseForm As String
    Public merchantLogo As String
    Public merchantBGImage As String
    Public enableHTTPTrans As String
    Public useHTTPReferrers As String

    Public OrigCompanyName As String
    Public OrigAccountName As String
    Public OrigCompanyAlias As String
    Public OrigAddress1 As String
    Public OrigAddress2 As String
    Public OrigCity As String
    Public OrigState As String
    Public OrigZipCode As String
    Public OrigContact_Name As String
    Public OrigContact_Phone1 As String
    Public OrigContact_Phone2 As String
    Public OrigContact_Email As String
    Public OrigMerchantURL As String
    Public OrigMerchantEmail As String
    Public OrigTerminalEmail As String
    Public OrigTerminalNumber As String
    Public OrigsPIN As String
    Public OrigActive As String
    Public Origrapprvlmethod As String
    Public OrigrapprvlgetURL As String
    Public OrigrapprvlPostURL As String
    Public OrigrapprvlText As String
    Public OrigrapprvlNotifyMerchant As String
    Public OrigrapprvlNotifyCustomer As String
    Public OrigrdeclMethod As String
    Public OrigrdeclGetURL As String
    Public OrigrdeclPostURL As String
    Public OrigrdeclText As String
    Public OrigrdeclNotifyMerchant As String
    Public OrigrdeclNotifyCustomer As String
    Public OrigautoPend As String
    Public OrigautoSettle As String
    Public OrigautoSettleTime As String
    Public OrigdateLastAutoSettle As Date
    Public OrigsettleInProgress As String
    Public OrigintegratePurchaseForm As String
    Public OrigmerchantLogo As String
    Public OrigmerchantBGImage As String
    Public OrigenableHTTPTrans As String
    Public OriguseHTTPReferrers As String

    Public market As String
    Public emp_UID As String
    Public DBConnectString As String
    Public ErrMsg As String

    Public ServerIP As String
    Public ServerTime As String
    Public SourceIP As String
    Public sPassWord As String
    Public SessionID As String
    Public MerchantType As String
    Public BatchSearch As Boolean
    Dim oAudit As gaAudit.AuditEvent

    Public Function AddMerchant(ByVal arrMerchant() As String) As String

        Dim sSQL As String
        Dim viaKLIXID As String = ""
        Dim Conn As SqlConnection
        Dim Cmd As SqlCommand
        Dim Rs As DataSet
        Dim bExistingMerchant As Boolean
        Dim ds As New DataSet
        Dim i As Integer
        Dim sPin As String = ""
        'Dim sNewPin As String
        Dim valpha As Object
        Dim nSides As Integer
        Dim GeneralResult As Integer
        'Dim vAchar As Object
        'Dim vAnum As Object
        'Dim validpwd2 As Integer
        'Dim validpwd1 As Integer
        'Dim valid1 As Boolean
        'Dim valid2 As Boolean
        'Dim a As Integer
        Dim sErr As String = ""
        'If sPassWord <> "" Then
        '    vAchar = Split("A,B,C,D,E,F,G,H,I,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,!,@,#,$,%,^,&,*,(),_,-,=,+,<,>,?,/,:,;,',[,],{,},|,\,`,~", ",")
        '    vAnum = Split("1,2,3,4,5,6,7,8,9,0", ",")

        '    For i = 0 To UBound(vAchar)
        '        validpwd1 = InStr(1, sPassWord, vAchar(i), vbTextCompare)
        '        If CLng(validpwd1) > 0 Then
        '            valid1 = True
        '            Exit For
        '        End If
        '    Next

        '    If valid1 Then
        '        For a = 0 To UBound(vAnum)
        '            validpwd2 = InStr(sPassWord, vAnum(a))
        '            If CLng(validpwd2) > 0 Then
        '                valid2 = True
        '                Exit For
        '            End If
        '        Next
        '        If Not valid2 Then
        '            ErrMsg = "Your password must contain at least one number."
        '            Exit Function
        '        End If
        '    Else
        '        If Not valid1 Then
        '            ErrMsg = "Your password must contain at least one letter."
        '            Exit Function
        '        End If
        '    End If

        '    'End If
        'End If
        bExistingMerchant = False
        'Conn = New SqlConnection
        Rs = New DataSet


        sSQL = "SELECT viaKlix_ID FROM TERMINALS WHERE Terminal_Number = " & CheckString(TerminalNumber)
        GetDataset(DBConnectString, sSQL, "terminal", sErr, Rs)

        If Rs.Tables(0).Rows.Count > 0 Then

            viaKLIXID = SafeString(Rs.Tables(0).Rows(0).Item("viaKlix_ID"))

        End If
        If viaKLIXID <> "" Then
            sSQL = "SELECT viaKlix_ID FROM MERCHANTS"
            sSQL = sSQL & " WHERE viaKlix_ID =" & CheckString(viaKLIXID)
            GetDataset(DBConnectString, sSQL, "merchant", sErr, ds)
            'Rs.Open(sSQL, Conn, adOpenForwardOnly, adLockReadOnly, adCmdText)
            If ds.Tables(0).Rows.Count > 0 Then
                sErr = "Enter a unique Terminal Number."
                bExistingMerchant = True
            ElseIf Len(sErr) > 0 Then
                LogErrorEvent(sErr)
            End If
        End If

        If Not bExistingMerchant Then

            i = 0
            '==xiuqin 2/11/02 default pin need to be alphanumeric
            If Len(sPin) < 6 Then
                valpha = Split("A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z", ",")
                nSides = 36
                For i = 0 To 6 - 1
                    Randomize()
                    GeneralResult = Int(nSides * Rnd())
                    If GeneralResult < 10 Then
                        sPin = sPin & GeneralResult
                    Else
                        sPin = sPin & valpha(GeneralResult - 10)
                    End If
                Next
            End If
            Cmd = New SqlCommand
            Conn = New SqlConnection
            With Cmd
                .Connection = Conn
                .CommandType = CommandType.StoredProcedure
                .CommandText = "CREATE_MERCHANT"
                .Parameters.Add(New SqlParameter("@i_merchantType", SqlDbType.VarChar, 15)).Value = MerchantType
                .Parameters.Add(New SqlParameter("@i_PASSWORD", SqlDbType.VarChar, 15)).Value = sPassWord
                .Parameters.Add(New SqlParameter("@i_cid", SqlDbType.Int, 5)).Value = CInt(CompanyID)
                .Parameters.Add(New SqlParameter("@i_accountname", SqlDbType.VarChar, 100)).Value = AccountName
                .Parameters.Add(New SqlParameter("@i_alias", SqlDbType.VarChar, 100)).Value = CompanyAlias
                .Parameters.Add(New SqlParameter("@i_merchantname", SqlDbType.VarChar, 100)).Value = CompanyName
                .Parameters.Add(New SqlParameter("@i_add1", SqlDbType.VarChar, 100)).Value = Address1
                .Parameters.Add(New SqlParameter("@i_add2", SqlDbType.VarChar, 100)).Value = Address2
                .Parameters.Add(New SqlParameter("@i_city", SqlDbType.VarChar, 50)).Value = City
                .Parameters.Add(New SqlParameter("@i_state", SqlDbType.VarChar, 2)).Value = State
                .Parameters.Add(New SqlParameter("@i_zip", SqlDbType.VarChar, 10)).Value = ZipCode
                .Parameters.Add(New SqlParameter("@i_contactName", SqlDbType.VarChar, 100)).Value = Contact_Name
                .Parameters.Add(New SqlParameter("@i_contactphone1", SqlDbType.VarChar, 20)).Value = Contact_Phone1
                .Parameters.Add(New SqlParameter("@i_contactphone2", SqlDbType.VarChar, 20)).Value = Contact_Phone2
                .Parameters.Add(New SqlParameter("@i_contactemail", SqlDbType.VarChar, 100)).Value = Contact_Email
                .Parameters.Add(New SqlParameter("@i_merchanturl", SqlDbType.VarChar, 255)).Value = MerchantURL
                .Parameters.Add(New SqlParameter("@i_terminalemail", SqlDbType.VarChar, 255)).Value = MerchantEmail
                .Parameters.Add(New SqlParameter("@i_terminalnumber", SqlDbType.VarChar, 22)).Value = TerminalNumber
                .Parameters.Add(New SqlParameter("@i_market", SqlDbType.VarChar, 100)).Value = market
                .Parameters.Add(New SqlParameter("@i_pin", SqlDbType.VarChar, 6)).Value = Left(sPin, 6)
                .Parameters.Add(New SqlParameter("@o_vid", SqlDbType.VarChar, 15)).Direction = ParameterDirection.Output
                .Parameters.Add(New SqlParameter("@o_ERRORMSG", SqlDbType.VarChar, 255)).Direction = ParameterDirection.Output

            End With
            Try
                Cmd.Connection.Open()
                Cmd.ExecuteNonQuery()

                '------------------------------------------------------------------------
                'Returns a message to the calling ASP page.
                '------------------------------------------------------------------------
                If Not IsNothing(Cmd.Parameters("o_ERRORMSG").Value) Then

                    'Debug.Print("Error Msg = " & Cmd.Parameters("o_ERRORMSG"))
                    ErrMsg = Cmd.Parameters("o_ERRORMSG").Value

                    'Cmd = Nothing
                    'Conn.Close()
                    'Conn = Nothing

                End If
                '------------------------------------------------------------------------
                viaKLIXID = Cmd.Parameters("o_vid").Value
                'viaKLIXID = Cmd(20)
                'If Not IsNull(Cmd.Parameters("o_ERRORMSG")) Then
                '   Debug.Print ("Error Msg = " & Cmd.Parameters("o_ERRORMSG"))
                '  Exit Function
                'Else
                'Cmd.ActiveConnection = Nothing
                If viaKLIXID <> "" Then
                    'oAudit = New gaAugit.AuditEvent ''CreateObject("viaAudit.AuditEvent")
                    oAudit.DBConnectString = DBConnectString
                    oAudit.SESSION_USER_ID = SESSION_USER_ID
                    oAudit.SESSION_GAPORTAL_ID = SESSION_VIAKLIX_ID
                    oAudit.SESSION_COMPANY_ID = SESSION_COMPANY_ID
                    oAudit.AFFECTED_USER_ID = viaKLIXID
                    oAudit.AFFECTED_GAPORTAL_ID = viaKLIXID
                    oAudit.AFFECTED_COMPANY_ID = CInt(CompanyID)
                    oAudit.AddInfo(68, ServerIP)
                    oAudit.AddInfo(69, ServerTime)
                    oAudit.AddInfo(70, SourceIP)
                    oAudit.EventID = 10  '-----  Merchant Creation
                    x = oAudit.SaveEvent()
                    oAudit = Nothing
                End If
                AddMerchant = viaKLIXID

            Catch ex As SqlException

                For i = 0 To ex.Errors.Count - 1
                    sErr = ex.Errors(i).Message.ToString()
                Next i
                ''/**use this code to debug**//

                LogErrorEvent(sErr)

            Finally
                If Not (Conn Is Nothing) Then
                    Conn.Close()
                End If
            End Try
        End If
        Return Nothing
    End Function

    Public Function AddMerchantConfirm(ByVal VID As String) As DataSet

        Dim sSQL As String
        'Dim bMerchantExit As Boolean

        Dim Rs As New DataSet
        Dim sErr As String = ""

        sSQL = ""
        sSQL = sSQL & "select "
        sSQL = sSQL & "     REBRAND_COMPANIES.COMPANY_NAME, "
        sSQL = sSQL & "     MERCHANTS.COMPANY_ID, "
        sSQL = sSQL & "     MERCHANTS.ACCOUNT_NAME, "
        sSQL = sSQL & "     TERMINALS.MERCHANT_NAME, "
        sSQL = sSQL & "     MERCHANTS.ALIAS, "
        sSQL = sSQL & "     TERMINALS.ADDRESS1, "
        sSQL = sSQL & "     TERMINALS.ADDRESS2, "
        sSQL = sSQL & "     TERMINALS.CITY, "
        sSQL = sSQL & "     TERMINALS.STATE, "
        sSQL = sSQL & "     TERMINALS.ZIP, "
        sSQL = sSQL & "     TERMINALS.CONTACT_NAME, "
        sSQL = sSQL & "     TERMINALS.CONTACT_PHONE1, "
        sSQL = sSQL & "     TERMINALS.CONTACT_PHONE2, "
        sSQL = sSQL & "     TERMINALS.CONTACT_EMAIL, "
        sSQL = sSQL & "     MERCHANTS.ACTIVE, "
        sSQL = sSQL & "     TERMINALS.MERCHANT_URL, "
        sSQL = sSQL & "     TERMINALS.TERMINAL_EMAIL, "
        sSQL = sSQL & "     USERS_TERMINALS.PIN "
        sSQL = sSQL & "from "
        sSQL = sSQL & "     MERCHANTS, USERS_TERMINALS, TERMINALS,REBRAND_COMPANIES "
        sSQL = sSQL & "where "
        sSQL = sSQL & "     MERCHANTS.Viaklix_ID = " & CheckString(VID) & " and "
        sSQL = sSQL & "     TERMINALS.Terminal_Number = " & CheckString(TerminalNumber) & " and "
        sSQL = sSQL & "     USERS_TERMINALS.COMPANY_ID = MERCHANTS.COMPANY_ID and "
        sSQL = sSQL & "     REBRAND_COMPANIES.COMPANY_ID = MERCHANTS.COMPANY_ID and "
        sSQL = sSQL & "     MERCHANTS.Viaklix_ID = TERMINALS.Viaklix_ID and "
        sSQL = sSQL & "     USERS_TERMINALS.TERMINAL_NUMBER = TERMINALS.TERMINAL_NUMBER and "
        If SESSION_USER_ID <> "" And SESSION_USER_ID <> VID Then
            sSQL = sSQL & "     USERS_TERMINALS.USER_ID = " & CheckString(SESSION_USER_ID)
        Else
            sSQL = sSQL & "     USERS_TERMINALS.USER_ID = " & CheckString(VID)
        End If
        'Rs.Open(sSQL, Conn, adOpenForwardOnly, adLockReadOnly, adCmdText)
        Rs = New DataSet
        Try
            GetDataset(DBConnectString, sSQL, "merchant", sErr, Rs)
            If Len(sErr) = 0 Or IsNothing(sErr) Then
                AddMerchantConfirm = Rs
            Else
                LogErrorEvent(sErr)
            End If
        Catch ex As SqlException

            Dim i As Integer = 0

            For i = 0 To ex.Errors.Count - 1
                sErr = ex.Errors(i).Message.ToString()
            Next i
            ''/**use this code to debug**//

            LogErrorEvent(sErr)


        End Try
        Return Nothing

    End Function


    Public Function EditMerchant(ByVal VID As String, _
            ByVal CID As String, _
            ByVal sPageid As String, _
            Optional ByVal TID As String = "") As Boolean
        Dim sErr As String = ""
        Dim sSQL As String
        Dim bUpdate As Boolean
        Dim bNeedToSave As Boolean
        Dim Conn As SqlConnection
        Dim Cmd As SqlCommand
        Dim Rs As DataSet

        If sPageid = "IMAGECONFIG" Then
            If Not Len(Trim(merchantLogo)) > 0 And Len(Trim(OrigmerchantLogo)) > 0 Then
                merchantLogo = OrigmerchantLogo
            ElseIf merchantLogo = "CLEAR_FROM_DB" Then
                merchantLogo = ""
            End If

            If Not Len(Trim(merchantBGImage)) > 0 And Len(Trim(OrigmerchantBGImage)) > 0 Then
                merchantBGImage = OrigmerchantBGImage
            ElseIf merchantBGImage = "CLEAR_FROM_DB" Then
                merchantBGImage = ""
            End If
            If Not Len(Trim(merchantLogo)) > 0 And Not Len(Trim(OrigmerchantLogo)) > 0 Or Not Len(Trim(merchantBGImage)) > 0 And Not Len(Trim(OrigmerchantBGImage)) > 0 Then
                'Conn = New ADODB.Connection
                Rs = New DataSet
                sSQL = ""
                sSQL = sSQL & "select "
                sSQL = sSQL & "Merchant_Logo, "
                sSQL = sSQL & "Merchant_BGImage "
                sSQL = sSQL & "From Terminals "
                sSQL = sSQL & "Where viaKlix_id =" & CheckString(VID) & " And "
                sSQL = sSQL & "Terminal_Number = " & CheckString(TID) & ""
                GetDataset(DBConnectString, sSQL, "merchant", sErr, Rs)
                'Rs.Open(sSQL, Conn, adOpenForwardOnly, adLockReadOnly, adCmdText)
                If Rs.Tables(0).Rows.Count > 0 Then
                    If Not Len(Trim(OrigmerchantLogo)) > 0 And Not Len(Trim(merchantLogo)) > 0 Then
                        OrigmerchantLogo = SafeString(Rs.Tables(0).Rows(0).Item("Merchant_Logo"))
                        merchantLogo = OrigmerchantLogo
                    ElseIf Not Len(Trim(OrigmerchantBGImage)) > 0 And Not Len(Trim(merchantBGImage)) > 0 Then
                        OrigmerchantBGImage = SafeString(Rs.Tables(0).Rows(0).Item("Merchant_BGImage"))
                        merchantBGImage = OrigmerchantBGImage
                    End If
                    'Else
                    '    EditMerchant = False
                End If
                'Rs.Close()
                'Rs.ActiveConnection = Nothing
                'Conn.Close()
                'Conn = Nothing
            End If
        End If

        'Conn = New ADODB.Connection
        'Conn.Open(DBConnectString)
        'Cmd = New ADODB.Command

        'Cmd.ActiveConnection = Conn
        'Cmd.CommandType = adCmdStoredProc
        'Cmd.CommandText = "VIAKLIX_MERCHANTMAINT.UPDMERCHANT"
        'Cmd.Parameters.Append(Cmd.CreateParameter("i_vid", adVarChar, adParamInput, 15, VID))
        'Cmd.Parameters.Append(Cmd.CreateParameter("i_tid", adVarChar, adParamInput, 22, TID))
        'Cmd.Parameters.Append(Cmd.CreateParameter("i_cid", adVarChar, adInteger, 5, CInt(CID)))
        'Cmd.Parameters.Append(Cmd.CreateParameter("i_pageid", adVarChar, adParamInput, 30, sPageid))
        'Cmd.Parameters.Append(Cmd.CreateParameter("i_accountname_old", adVarChar, adParamInput, 100, OrigAccountName))
        'Cmd.Parameters.Append(Cmd.CreateParameter("i_accountname_new", adVarChar, adParamInput, 100, AccountName))
        'Cmd.Parameters.Append(Cmd.CreateParameter("i_alias_old", adVarChar, adParamInput, 100, OrigCompanyAlias))
        'Cmd.Parameters.Append(Cmd.CreateParameter("i_alias_new", adVarChar, adParamInput, 100, CompanyAlias))
        'Cmd.Parameters.Append(Cmd.CreateParameter("i_merchantname_old", adVarChar, adParamInput, 100, OrigCompanyName))
        'Cmd.Parameters.Append(Cmd.CreateParameter("i_merchantname_new", adVarChar, adParamInput, 100, CompanyName))
        'Cmd.Parameters.Append(Cmd.CreateParameter("i_add1_old", adVarChar, adParamInput, 100, OrigAddress1))
        'Cmd.Parameters.Append(Cmd.CreateParameter("i_add1_new", adVarChar, adParamInput, 100, Address1))
        'Cmd.Parameters.Append(Cmd.CreateParameter("i_add2_old", adVarChar, adParamInput, 100, OrigAddress2))
        'Cmd.Parameters.Append(Cmd.CreateParameter("i_add2_new", adVarChar, adParamInput, 100, Address2))
        'Cmd.Parameters.Append(Cmd.CreateParameter("i_city_old", adVarChar, adParamInput, 50, OrigCity))
        'Cmd.Parameters.Append(Cmd.CreateParameter("i_city_new", adVarChar, adParamInput, 50, City))
        'Cmd.Parameters.Append(Cmd.CreateParameter("i_state_old", adVarChar, adParamInput, 2, OrigState))
        'Cmd.Parameters.Append(Cmd.CreateParameter("i_state_new", adVarChar, adParamInput, 2, State))
        'Cmd.Parameters.Append(Cmd.CreateParameter("i_zip_old", adVarChar, adParamInput, 10, OrigZipCode))
        'Cmd.Parameters.Append(Cmd.CreateParameter("i_zip_new", adVarChar, adParamInput, 10, ZipCode))
        'Cmd.Parameters.Append(Cmd.CreateParameter("i_contactName_old", adVarChar, adParamInput, 100, OrigContact_Name))
        'Cmd.Parameters.Append(Cmd.CreateParameter("i_contactName_new", adVarChar, adParamInput, 100, Contact_Name))
        'Cmd.Parameters.Append(Cmd.CreateParameter("i_contactphone1_old", adVarChar, adParamInput, 20, OrigContact_Phone1))
        'Cmd.Parameters.Append(Cmd.CreateParameter("i_contactphone1_new", adVarChar, adParamInput, 20, Contact_Phone1))
        'Cmd.Parameters.Append(Cmd.CreateParameter("i_contactphone2_old", adVarChar, adParamInput, 20, OrigContact_Phone2))
        'Cmd.Parameters.Append(Cmd.CreateParameter("i_contactphone1_new", adVarChar, adParamInput, 20, Contact_Phone2))
        'Cmd.Parameters.Append(Cmd.CreateParameter("i_contactemail_old", adVarChar, adParamInput, 100, OrigContact_Email))
        'Cmd.Parameters.Append(Cmd.CreateParameter("i_contactemail_new", adVarChar, adParamInput, 100, Contact_Email))
        'Cmd.Parameters.Append(Cmd.CreateParameter("i_active_old", adVarChar, adParamInput, 1, OrigActive))
        'Cmd.Parameters.Append(Cmd.CreateParameter("i_active_new", adVarChar, adParamInput, 1, Active))
        'Cmd.Parameters.Append(Cmd.CreateParameter("i_merchanturl_old", adVarChar, adParamInput, 255, OrigMerchantURL))
        'Cmd.Parameters.Append(Cmd.CreateParameter("i_merchanturl_new", adVarChar, adParamInput, 255, MerchantURL))
        'Cmd.Parameters.Append(Cmd.CreateParameter("i_rapprvlmethod_old", adVarChar, adParamInput, 4, Origrapprvlmethod))
        'Cmd.Parameters.Append(Cmd.CreateParameter("i_rapprvlmethod_new", adVarChar, adParamInput, 4, rapprvlmethod))
        'Cmd.Parameters.Append(Cmd.CreateParameter("i_rapprvlgetURL_old", adVarChar, adParamInput, 255, OrigrapprvlgetURL))
        'Cmd.Parameters.Append(Cmd.CreateParameter("i_rapprvlgetURL_new", adVarChar, adParamInput, 255, rapprvlgetURL))
        'Cmd.Parameters.Append(Cmd.CreateParameter("i_rapprvlPostURL_old ", adVarChar, adParamInput, 255, OrigrapprvlPostURL))
        'Cmd.Parameters.Append(Cmd.CreateParameter("i_rapprvlPostURL_new", adVarChar, adParamInput, 255, rapprvlPostURL))
        'Cmd.Parameters.Append(Cmd.CreateParameter("i_rapprvlText_old ", adVarChar, adParamInput, 100, OrigrapprvlText))
        'Cmd.Parameters.Append(Cmd.CreateParameter("i_rapprvlText_new ", adVarChar, adParamInput, 100, rapprvlText))
        'Cmd.Parameters.Append(Cmd.CreateParameter("i_rapprvlNotifyMerchant_old ", adVarChar, adParamInput, 1, OrigrapprvlNotifyMerchant))
        'Cmd.Parameters.Append(Cmd.CreateParameter("i_rapprvlNotifyMerchant_new ", adVarChar, adParamInput, 1, rapprvlNotifyMerchant))
        'Cmd.Parameters.Append(Cmd.CreateParameter("i_rapprvlNotifyCustomer_old ", adVarChar, adParamInput, 1, OrigrapprvlNotifyCustomer))
        'Cmd.Parameters.Append(Cmd.CreateParameter("i_rapprvlNotifyCustomer_new", adVarChar, adParamInput, 1, rapprvlNotifyCustomer))
        'Cmd.Parameters.Append(Cmd.CreateParameter("i_rdeclMethod_old ", adVarChar, adParamInput, 4, OrigrdeclMethod))
        'Cmd.Parameters.Append(Cmd.CreateParameter("i_rdeclMethod_new ", adVarChar, adParamInput, 4, rdeclMethod))
        'Cmd.Parameters.Append(Cmd.CreateParameter("i_rdeclGetURL_old ", adVarChar, adParamInput, 255, OrigrdeclGetURL))
        'Cmd.Parameters.Append(Cmd.CreateParameter("i_rdeclGetURL_new", adVarChar, adParamInput, 255, rdeclGetURL))
        'Cmd.Parameters.Append(Cmd.CreateParameter("i_rdeclPostURL_old ", adVarChar, adParamInput, 255, OrigrdeclPostURL))
        'Cmd.Parameters.Append(Cmd.CreateParameter("i_rdeclPostURL_new", adVarChar, adParamInput, 255, rdeclPostURL))
        'Cmd.Parameters.Append(Cmd.CreateParameter("i_rdeclText_old ", adVarChar, adParamInput, 100, OrigrdeclText))
        'Cmd.Parameters.Append(Cmd.CreateParameter("i_rdeclText_new", adVarChar, adParamInput, 100, rdeclText))
        'Cmd.Parameters.Append(Cmd.CreateParameter("i_rdeclNotifyMerchant_old ", adVarChar, adParamInput, 1, OrigrdeclNotifyMerchant))
        'Cmd.Parameters.Append(Cmd.CreateParameter("i_rdeclNotifyMerchant_new ", adVarChar, adParamInput, 1, rdeclNotifyMerchant))
        'Cmd.Parameters.Append(Cmd.CreateParameter("i_rdeclNotifyCustomer_old ", adVarChar, adParamInput, 1, OrigrdeclNotifyCustomer))
        'Cmd.Parameters.Append(Cmd.CreateParameter("i_rdeclNotifyCustomer_new", adVarChar, adParamInput, 1, rdeclNotifyCustomer))
        'Cmd.Parameters.Append(Cmd.CreateParameter("i_terminalEmail_old", adVarChar, adParamInput, 255, OrigTerminalEmail))
        'Cmd.Parameters.Append(Cmd.CreateParameter("i_terminalEmail_new", adVarChar, adParamInput, 255, TerminalEmail))
        'Cmd.Parameters.Append(Cmd.CreateParameter("i_autoPend_old ", adVarChar, adParamInput, 1, OrigautoPend))
        'Cmd.Parameters.Append(Cmd.CreateParameter("i_autoPend_new ", adVarChar, adParamInput, 1, autoPend))
        'Cmd.Parameters.Append(Cmd.CreateParameter("i_autoSettle_old ", adVarChar, adParamInput, 100, OrigautoSettle))
        'Cmd.Parameters.Append(Cmd.CreateParameter("i_autoSettle_new", adVarChar, adParamInput, 100, autoSettle))
        'Cmd.Parameters.Append(Cmd.CreateParameter("i_autoSettleTime_old", adVarChar, adParamInput, 4, OrigautoSettleTime))
        'Cmd.Parameters.Append(Cmd.CreateParameter("i_autoSettleTime_new", adVarChar, adParamInput, 4, autoSettleTime))
        'Cmd.Parameters.Append(Cmd.CreateParameter("i_dateLastAutoSettle_old", adDate, adParamInput, 4, OrigdateLastAutoSettle))
        'Cmd.Parameters.Append(Cmd.CreateParameter("i_dateLastAutoSettle_new", adDate, adParamInput, 4, dateLastAutoSettle))
        'Cmd.Parameters.Append(Cmd.CreateParameter("i_settleInProgress_old", adVarChar, adParamInput, 1, OrigsettleInProgress))
        'Cmd.Parameters.Append(Cmd.CreateParameter("i_settleInProgress_new", adVarChar, adParamInput, 1, settleInProgress))
        'Cmd.Parameters.Append(Cmd.CreateParameter("i_integratePurchaseForm_old", adVarChar, adParamInput, 1, OrigintegratePurchaseForm))
        'Cmd.Parameters.Append(Cmd.CreateParameter("i_integratePurchaseForm_new", adVarChar, adParamInput, 1, integratePurchaseForm))
        'Cmd.Parameters.Append(Cmd.CreateParameter("i_merchantLogo_old", adVarChar, adParamInput, 255, OrigmerchantLogo))
        'Cmd.Parameters.Append(Cmd.CreateParameter("i_merchantLogo_new", adVarChar, adParamInput, 255, merchantLogo))
        'Cmd.Parameters.Append(Cmd.CreateParameter("i_merchantBGImage_old", adVarChar, adParamInput, 255, OrigmerchantBGImage))
        'Cmd.Parameters.Append(Cmd.CreateParameter("i_merchantBGImage_new", adVarChar, adParamInput, 255, merchantBGImage))
        'Cmd.Parameters.Append(Cmd.CreateParameter("i_enableHTTPTrans_old", adVarChar, adParamInput, 1, OrigenableHTTPTrans))
        'Cmd.Parameters.Append(Cmd.CreateParameter("i_enableHTTPTrans_new", adVarChar, adParamInput, 1, enableHTTPTrans))
        'Cmd.Parameters.Append(Cmd.CreateParameter("i_useHTTPReferrers_old", adVarChar, adParamInput, 255, OriguseHTTPReferrers))
        'Cmd.Parameters.Append(Cmd.CreateParameter("i_useHTTPReferrers_new", adVarChar, adParamInput, 255, useHTTPReferrers))
        'Cmd.Parameters.Append(Cmd.CreateParameter("o_ERRORMSG", adVarChar, adParamInputOutput, 255, " "))
        'Cmd.Execute()

        '------------------------------------------------------------------------
        'Returns a message to the calling ASP page.
        '------------------------------------------------------------------------
        Cmd = New SqlCommand
        Conn = New SqlConnection
        With Cmd
            .Connection = Conn
            .CommandType = CommandType.StoredProcedure
            .CommandText = "UPDMERCHANT"
            .Parameters.Add(New SqlParameter("@i_vid", SqlDbType.VarChar, 15)).Value = VID
            .Parameters.Add(New SqlParameter("@i_tid", SqlDbType.VarChar, 22)).Value = TID
            .Parameters.Add(New SqlParameter("@i_cid", SqlDbType.Int, 5)).Value = CInt(CID)
            .Parameters.Add(New SqlParameter("@i_pageid", SqlDbType.VarChar, 30)).Value = sPageid
            .Parameters.Add(New SqlParameter("@i_accountname_old", SqlDbType.VarChar, 100)).Value = OrigAccountName
            .Parameters.Add(New SqlParameter("@i_accountname_new", SqlDbType.VarChar, 100)).Value = AccountName

            .Parameters.Add(New SqlParameter("@i_alias_old", SqlDbType.VarChar, 100)).Value = OrigCompanyAlias
            .Parameters.Add(New SqlParameter("@i_alias_new", SqlDbType.VarChar, 100)).Value = CompanyAlias

            .Parameters.Add(New SqlParameter("@i_merchantname_old", SqlDbType.VarChar, 100)).Value = OrigCompanyName
            .Parameters.Add(New SqlParameter("@i_merchantname_new", SqlDbType.VarChar, 100)).Value = CompanyName

            .Parameters.Add(New SqlParameter("@i_add1_old", SqlDbType.VarChar, 100)).Value = OrigAddress1
            .Parameters.Add(New SqlParameter("@i_add1_new", SqlDbType.VarChar, 100)).Value = Address1

            .Parameters.Add(New SqlParameter("@i_add2_old", SqlDbType.VarChar, 100)).Value = OrigAddress2
            .Parameters.Add(New SqlParameter("@i_add2_new", SqlDbType.VarChar, 100)).Value = Address2

            .Parameters.Add(New SqlParameter("@i_city_old", SqlDbType.VarChar, 50)).Value = OrigCity
            .Parameters.Add(New SqlParameter("@i_city_new", SqlDbType.VarChar, 50)).Value = City

            .Parameters.Add(New SqlParameter("@i_state_old", SqlDbType.VarChar, 2)).Value = OrigState
            .Parameters.Add(New SqlParameter("@i_state_new", SqlDbType.VarChar, 2)).Value = State

            .Parameters.Add(New SqlParameter("@i_zip_old", SqlDbType.VarChar, 10)).Value = OrigZipCode
            .Parameters.Add(New SqlParameter("@i_zip_new", SqlDbType.VarChar, 10)).Value = ZipCode

            .Parameters.Add(New SqlParameter("@i_contactName_old", SqlDbType.VarChar, 100)).Value = OrigContact_Name
            .Parameters.Add(New SqlParameter("@i_contactName_new", SqlDbType.VarChar, 100)).Value = Contact_Name

            .Parameters.Add(New SqlParameter("@i_contactphone1_old", SqlDbType.VarChar, 20)).Value = OrigContact_Phone1
            .Parameters.Add(New SqlParameter("@i_contactphone1_new", SqlDbType.VarChar, 20)).Value = Contact_Phone1
            .Parameters.Add(New SqlParameter("@i_contactphone2_old", SqlDbType.VarChar, 20)).Value = OrigContact_Phone2
            .Parameters.Add(New SqlParameter("@i_contactphone2_new", SqlDbType.VarChar, 20)).Value = Contact_Phone2




            .Parameters.Add(New SqlParameter("@i_contactemail_old", SqlDbType.VarChar, 100)).Value = OrigContact_Email
            .Parameters.Add(New SqlParameter("@i_contactemail_new", SqlDbType.VarChar, 100)).Value = Contact_Email
            .Parameters.Add(New SqlParameter("@i_active_old", SqlDbType.VarChar, 1)).Value = OrigActive
            .Parameters.Add(New SqlParameter("@i_active_new", SqlDbType.VarChar, 1)).Value = Active
            .Parameters.Add(New SqlParameter("@i_merchanturl_old", SqlDbType.VarChar, 255)).Value = OrigMerchantURL
            .Parameters.Add(New SqlParameter("@i_merchanturl_new", SqlDbType.VarChar, 255)).Value = MerchantURL
            .Parameters.Add(New SqlParameter("@i_rapprvlmethod_old", SqlDbType.VarChar, 4)).Value = Origrapprvlmethod
            .Parameters.Add(New SqlParameter("@i_rapprvlmethod_new", SqlDbType.VarChar, 4)).Value = rapprvlmethod
            .Parameters.Add(New SqlParameter("@i_rapprvlgetURL_old", SqlDbType.VarChar, 255)).Value = OrigrapprvlgetURL
            .Parameters.Add(New SqlParameter("@i_rapprvlgetURL_new", SqlDbType.VarChar, 255)).Value = rapprvlgetURL
            .Parameters.Add(New SqlParameter("@i_rapprvlPostURL_old ", SqlDbType.VarChar, 255)).Value = OrigrapprvlPostURL
            .Parameters.Add(New SqlParameter("@i_rapprvlPostURL_new", SqlDbType.VarChar, 255)).Value = rapprvlPostURL
            .Parameters.Add(New SqlParameter("@i_rapprvlText_old ", SqlDbType.VarChar, 100)).Value = OrigrapprvlText
            .Parameters.Add(New SqlParameter("@i_rapprvlText_new ", SqlDbType.VarChar, 100)).Value = rapprvlText
            .Parameters.Add(New SqlParameter("@i_rapprvlNotifyMerchant_old ", SqlDbType.VarChar, 1)).Value = OrigrapprvlNotifyMerchant
            .Parameters.Add(New SqlParameter("@i_rapprvlNotifyMerchant_new ", SqlDbType.VarChar, 1)).Value = rapprvlNotifyMerchant
            .Parameters.Add(New SqlParameter("@i_rapprvlNotifyCustomer_old ", SqlDbType.VarChar, 1)).Value = OrigrapprvlNotifyCustomer
            .Parameters.Add(New SqlParameter("@i_rapprvlNotifyCustomer_new", SqlDbType.VarChar, 1)).Value = rapprvlNotifyCustomer
            .Parameters.Add(New SqlParameter("@i_rdeclMethod_old ", SqlDbType.VarChar, 4)).Value = OrigrdeclMethod
            .Parameters.Add(New SqlParameter("@i_rdeclMethod_new ", SqlDbType.VarChar, 4)).Value = rdeclMethod
            .Parameters.Add(New SqlParameter("@i_rdeclGetURL_old ", SqlDbType.VarChar, 255)).Value = OrigrdeclGetURL
            .Parameters.Add(New SqlParameter("@i_rdeclGetURL_new", SqlDbType.VarChar, 255)).Value = rdeclGetURL
            .Parameters.Add(New SqlParameter("@i_rdeclPostURL_old ", SqlDbType.VarChar, 255)).Value = OrigrdeclPostURL
            .Parameters.Add(New SqlParameter("@i_rdeclPostURL_new", SqlDbType.VarChar, 255)).Value = rdeclPostURL
            .Parameters.Add(New SqlParameter("@i_rdeclText_old ", SqlDbType.VarChar, 100)).Value = OrigrdeclText
            .Parameters.Add(New SqlParameter("@i_rdeclText_new", SqlDbType.VarChar, 100)).Value = rdeclText
            .Parameters.Add(New SqlParameter("@i_rdeclNotifyMerchant_old ", SqlDbType.VarChar, 1)).Value = OrigrdeclNotifyMerchant
            .Parameters.Add(New SqlParameter("@i_rdeclNotifyMerchant_new ", SqlDbType.VarChar, 1)).Value = rdeclNotifyMerchant
            .Parameters.Add(New SqlParameter("@i_rdeclNotifyCustomer_old ", SqlDbType.VarChar, 1)).Value = OrigrdeclNotifyCustomer
            .Parameters.Add(New SqlParameter("@i_rdeclNotifyCustomer_new", SqlDbType.VarChar, 1)).Value = rdeclNotifyCustomer
            .Parameters.Add(New SqlParameter("@i_terminalEmail_old", SqlDbType.VarChar, 255)).Value = OrigTerminalEmail
            .Parameters.Add(New SqlParameter("@i_terminalEmail_new", SqlDbType.VarChar, 255)).Value = TerminalEmail
            .Parameters.Add(New SqlParameter("@i_autoPend_old ", SqlDbType.VarChar, 1)).Value = OrigautoPend
            .Parameters.Add(New SqlParameter("@i_autoPend_new ", SqlDbType.VarChar, 1)).Value = autoPend
            .Parameters.Add(New SqlParameter("@i_autoSettle_old ", SqlDbType.VarChar, 100)).Value = OrigautoSettle
            .Parameters.Add(New SqlParameter("@i_autoSettle_new", SqlDbType.VarChar, 100)).Value = autoSettle
            .Parameters.Add(New SqlParameter("@i_autoSettleTime_old", SqlDbType.VarChar, 4)).Value = OrigautoSettleTime
            .Parameters.Add(New SqlParameter("@i_autoSettleTime_new", SqlDbType.VarChar, 4)).Value = autoSettleTime
            .Parameters.Add(New SqlParameter("@i_dateLastAutoSettle_old", SqlDbType.DateTime, 4)).Value = OrigdateLastAutoSettle
            .Parameters.Add(New SqlParameter("@i_dateLastAutoSettle_new", SqlDbType.DateTime, 4)).Value = dateLastAutoSettle
            .Parameters.Add(New SqlParameter("@i_settleInProgress_old", SqlDbType.VarChar, 1)).Value = OrigsettleInProgress
            .Parameters.Add(New SqlParameter("@i_settleInProgress_new", SqlDbType.VarChar, 1)).Value = settleInProgress
            .Parameters.Add(New SqlParameter("@i_integratePurchaseForm_old", SqlDbType.VarChar, 1)).Value = OrigintegratePurchaseForm
            .Parameters.Add(New SqlParameter("@i_integratePurchaseForm_new", SqlDbType.VarChar, 1)).Value = integratePurchaseForm
            .Parameters.Add(New SqlParameter("@i_merchantLogo_old", SqlDbType.VarChar, 255)).Value = OrigmerchantLogo
            .Parameters.Add(New SqlParameter("@i_merchantLogo_new", SqlDbType.VarChar, 255)).Value = merchantLogo
            .Parameters.Add(New SqlParameter("@i_merchantBGImage_old", SqlDbType.VarChar, 255)).Value = OrigmerchantBGImage
            .Parameters.Add(New SqlParameter("@i_merchantBGImage_new", SqlDbType.VarChar, 255)).Value = merchantBGImage
            .Parameters.Add(New SqlParameter("@i_enableHTTPTrans_old", SqlDbType.VarChar, 1)).Value = OrigenableHTTPTrans
            .Parameters.Add(New SqlParameter("@i_enableHTTPTrans_new", SqlDbType.VarChar, 1)).Value = enableHTTPTrans
            .Parameters.Add(New SqlParameter("@i_useHTTPReferrers_old", SqlDbType.VarChar, 255)).Value = OriguseHTTPReferrers
            .Parameters.Add(New SqlParameter("@i_useHTTPReferrers_new", SqlDbType.VarChar, 255)).Value = useHTTPReferrers
            .Parameters.Add(New SqlParameter("o_ERRORMSG", SqlDbType.VarChar, 255)).Direction = ParameterDirection.Output
        End With
        Cmd.Connection.Open()
        Cmd.ExecuteNonQuery()
        If Not IsNothing(Cmd.Parameters("o_ERRORMSG").Value) Then

            'Debug.Print("Error Msg = " & Cmd.Parameters("o_ERRORMSG"))
            ErrMsg = Cmd.Parameters("o_ERRORMSG").Value

            'Cmd = Nothing
            'Conn.Close()
            'Conn = Nothing

        End If
        '------------------------------------------------------------------------

        ' Cmd.ActiveConnection = Nothing
        bUpdate = True
        'Conn.Close()
        'Conn = Nothing

        'oAudit = New gaAugit.AuditEvent 'CreateObject("viaAudit.AuditEvent")
        oAudit.DBConnectString = DBConnectString
        oAudit.SESSION_USER_ID = SESSION_USER_ID
        oAudit.SESSION_GAPORTAL_ID = SESSION_VIAKLIX_ID
        oAudit.SESSION_COMPANY_ID = SESSION_COMPANY_ID
        oAudit.AFFECTED_USER_ID = VID
        oAudit.AFFECTED_GAPORTAL_ID = VID
        oAudit.AFFECTED_COMPANY_ID = CInt(CID)
        oAudit.AddInfo(68, ServerIP)
        oAudit.AddInfo(69, ServerTime)
        oAudit.AddInfo(70, SourceIP)
        If Len(TID) > 0 Then
            oAudit.AddInfo(75, TID)
        End If
        'If sPageid = "EditMerchant" Then
        If OrigActive = "Y" And Active = "N" Then

            '-----  saving merchant disablement event
            oAudit.EventID = 11 '-----  Merchant Disablement
            x = oAudit.SaveEvent()
            oAudit = Nothing
        End If
        If OrigActive = "N" And Active = "Y" Then
            oAudit.EventID = 10  '-----  Merchant Creation
            x = oAudit.SaveEvent()
            oAudit = Nothing
        End If
        'End If
        '==================================================================
        'Upload image file to upload old file
        If sPageid = "IMAGECONFIG" Then
            bNeedToSave = False

            '           -----   saving image upload event
            oAudit.EventID = 14 '-----  Image Uploads

            If merchantLogo <> "" And OrigmerchantLogo <> merchantLogo Then
                oAudit.AddInfo(62, SafeString(OrigmerchantLogo))
                oAudit.AddInfo(64, SafeString(merchantLogo))
                bNeedToSave = True
            End If
            If merchantBGImage <> "" And OrigmerchantBGImage <> merchantBGImage Then
                oAudit.AddInfo(63, SafeString(OrigmerchantBGImage))
                oAudit.AddInfo(65, SafeString(merchantBGImage))
                bNeedToSave = True
            End If
            If bNeedToSave Then
                x = oAudit.SaveEvent
            End If
            oAudit = Nothing
        End If

        If sPageid = "AutosettleConfig" Then

            oAudit.EventID = 12   '-----  Merchant Auto-Settle Auto-Pend Change
            bNeedToSave = False
            If autoPend <> OrigautoPend Then
                bNeedToSave = True
                oAudit.AddInfo(55, CStr(OrigautoPend))
                oAudit.AddInfo(59, CStr(autoPend))
            End If

            If autoSettle <> OrigautoSettle Then
                bNeedToSave = True
                oAudit.AddInfo(57, CStr(OrigautoSettle))
                oAudit.AddInfo(61, CStr(autoSettle))
            End If

            If autoSettleTime <> OrigautoSettleTime Then
                bNeedToSave = True
                oAudit.AddInfo(54, CStr(OrigautoSettleTime))
                oAudit.AddInfo(58, CStr(autoSettleTime))
            End If

            If bNeedToSave Then
                x = oAudit.SaveEvent
            End If

            oAudit = Nothing
        End If
        oAudit = Nothing
        'End If
        EditMerchant = bUpdate
        Exit Function
        'ErrorHandler:
        '        Call LogErrorEvent("AdminMerchant.EditMerchant")

        '        If Not Conn Is Nothing Then
        '            If Conn.State <> adStateClosed Then
        '                Conn.Close()
        '            End If

        '            Conn = Nothing
        '        End If

        '        oAudit = Nothing
        '        Cmd = Nothing
    End Function

    Public Function EditAccountConfirm(ByVal VID As String) As DataSet

        Dim sSQL As String
        'Dim Conn As ADODB.Connection
        Dim Rs As DataSet
        Dim sErr As String = ""
        'Conn = New ADODB.Connection
        Rs = New DataSet

        'Conn.Open(DBConnectString)
        'Rs.ActiveConnection = Conn
        'Rs.CursorLocation = adUseClient

        sSQL = ""
        sSQL = sSQL & "select "
        sSQL = sSQL & "     MERCHANTS.COMPANY_ID, "
        sSQL = sSQL & "     MERCHANTS.ACCOUNT_NAME, "
        sSQL = sSQL & "     MERCHANTS.ALIAS, "
        sSQL = sSQL & "     MERCHANTS.ACTIVE "
        sSQL = sSQL & "from "
        sSQL = sSQL & "     MERCHANTS "
        sSQL = sSQL & "where "
        sSQL = sSQL & "     MERCHANTS.Viaklix_ID = " & CheckString(VID)
        GetDataset(DBConnectString, sSQL, "merchant", sErr, Rs)
        'Rs.Open(sSQL, Conn, adOpenForwardOnly, adLockReadOnly, adCmdText)
        If Rs.Tables(0).Rows.Count > 0 Then
            EditAccountConfirm = Rs

        End If

        '        Conn.Close()
        '        Conn = Nothing
        '        Exit Function
        'ErrorHandler:
        '        Call LogErrorEvent("merchantmanage.EditAccountConfirm")

        '        If Not Rs Is Nothing Then
        '            If Rs.State <> adStateClosed Then
        '                Rs.Close()
        '            End If

        '            Rs = Nothing
        '        End If

        '        If Not Conn Is Nothing Then
        '            If Conn.State <> adStateClosed Then
        '                Conn.Close()
        '            End If

        '            Conn = Nothing
        '        End If
        Return Nothing
    End Function


    Public Function FindMerchant( _
    ByVal VID As String, _
    ByVal CID As Integer, _
    ByVal CompanyName As String, _
    ByVal Active As String, _
    ByVal SortBy As String, _
    ByVal ToggleSortOrder As Boolean) As Object

        Dim sSQL As String
        'Dim vMerchantList() As Array
        Dim SortOrder As String = ""
        Dim Conn As SqlConnection
        Dim Rs As DataSet
        Dim sErr As String = ""
        Conn = New SqlConnection
        Rs = New DataSet
        Dim pagesize As Integer
        ''Conn.Open(DBConnectString)
        'Rs.ActiveConnection = Conn
        'Rs.CursorLocation = adUseClient
        'Rs.Source = sSQL

        SaveSortField(SortBy, SortOrder, ToggleSortOrder)

        sSQL = "SELECT distinct t.Viaklix_ID, m.Company_ID, t.Merchant_Name, " & _
            "t.City, t.Contact_Phone1, t.Contact_Email, m.Active " & _
            "FROM MERCHANTS m, terminals t " & _
            "WHERE m.viaklix_ID = t.viaklix_ID "

        If SafeString(Active) <> "" Then
            sSQL = sSQL & " AND upper(m.Active) = '" & UCase(Active) & "'"
        End If
        If CID > 0 Then
            sSQL = sSQL & " AND m.Company_ID = " & CID & " "
        End If
        If Trim(VID) <> "" Then
            'sSQL = sSQL & " AND (t.viaklix_Id = '" & VID & "' or t.viaklix_Id like '" & UCase(Mid(VID, 1, 1)) & Mid(VID, 2) & "%' or t.Viaklix_ID like '%" & LCase(Mid(VID, 1, 1)) & Mid(VID, 2) & "%')"
            sSQL = sSQL & " AND UPPER(t.viaklix_ID) LIKE '%" & UCase(VID) & "%'"
        End If
        If Trim(CompanyName) <> "" Then
            'sSQL = sSQL & " AND (t.Merchant_Name = '" & CompanyName & "' or t.Merchant_Name like '" & UCase(Mid(CompanyName, 1, 1)) & Mid(CompanyName, 2) & "%' or t.Merchant_Name like '%" & LCase(Mid(CompanyName, 1, 1)) & Mid(CompanyName, 2) & "%')"
            sSQL = sSQL & " AND UPPER(t.Merchant_Name) LIKE '%" & UCase(CompanyName) & "%'"
        End If
        If Trim(SortBy) <> "" Then
            Select Case UCase(SortBy)
                Case "VIAKLIX_ID"
                    SortBy = "t." & SortBy
                Case "MERCHANT_NAME"
                    SortBy = "t.Merchant_Name"
                Case "CITY"
                    SortBy = "t." & SortBy
                Case "CONTACT_PHONE1"
                    SortBy = "t." & SortBy
                Case "CONTACT_EMAIL"
                    SortBy = "t." & SortBy
                Case "ACTIVE"
                    SortBy = "m." & SortBy
            End Select

            sSQL = sSQL & " order by upper(" & SortBy & ")" & SortOrder
        Else
            sSQL = sSQL & " order by t.viaKlix_ID " & SortOrder
        End If
        GetDataset(DBConnectString, sSQL, "merchant", sErr, Rs)
        'Debug.Print sSQL
        'Rs.Open(sSQL, Conn, adOpenStatic, adLockReadOnly, adCmdText)
        If IsNothing(nPageRecords) Then
            nPageRecords = 25
        Else
            pagesize = nPageRecords
        End If
        If Rs.Tables(0).Rows.Count > 0 Then
            nNumPages = Rs.Tables(0).Rows.Count / pagesize
            If nPage < 1 Then nPage = 1
            If nPage > nNumPages Then nPage = nNumPages

            '' Rs.Move (nPage - 1) * nPageRecords, 1
            'vMerchantList = Rs '.GetRows(nPageRecords)

            'If nPage > Rs.PageCount Then
            '    Rs.AbsolutePage = Rs.PageCount
            '    nPage = Rs.PageCount
            'Else
            '    Rs.AbsolutePage = nPage
            'End If
            ''Set FindMerchant = Rs
            'Rs.ActiveConnection = Nothing
        End If

        Conn.Close()
        Conn = Nothing
        'FindMerchant = vMerchantList

        'ErrorHandler:
        '        Call LogErrorEvent("AdminMerchant.FindMerchant")

        '        If Not Rs Is Nothing Then
        '            If Rs.State <> adStateClosed Then
        '                Rs.Close()
        '            End If

        '            Rs = Nothing
        '        End If

        '        If Not Conn Is Nothing Then
        '            If Conn.State <> adStateClosed Then
        '                Conn.Close()
        '            End If

        '            Conn = Nothing
        '        End If
        Return Nothing
    End Function
    Public Function DeleteMerchant(ByVal viaKLIXID As String) As Boolean
        ''On Error GoTo ErrorHandler
        Dim Conn As SqlConnection
        Dim Cmd As SqlCommand
        Conn = New SqlConnection
        'Conn.Open(DBConnectString)
        Cmd = New SqlCommand
        With Cmd
            .Connection = Conn
            .CommandType = CommandType.StoredProcedure
            .CommandText = "VIAKLIX_MERCHANTMAINT.DELETE_MERCHANT"
            .Parameters.Add(New SqlParameter("@i_vid", SqlDbType.VarChar, 15)).Value = viaKLIXID
            .Parameters.Add(New SqlParameter("@o_ERRORMSG", SqlDbType.VarChar, 255)).Direction = ParameterDirection.Output
        End With
        Cmd.Connection.Open()
        Cmd.ExecuteNonQuery()

        '------------------------------------------------------------------------
        'Returns a message to the calling ASP page.
        '------------------------------------------------------------------------
        If Not IsNothing(Cmd.Parameters("o_ERRORMSG").Value) Then

            'Debug.Print("Error Msg = " & Cmd.Parameters("o_ERRORMSG"))
            ErrMsg = Cmd.Parameters("o_ERRORMSG").Value

            Cmd = Nothing
            Conn.Close()
            Conn = Nothing

        End If
        '------------------------------------------------------------------------

        'If Not IsNull(Cmd.Parameters("o_ERRORMSG")) Then
        '    Debug.Print ("Error Msg = " & Cmd.Parameters("o_ERRORMSG"))
        '    DeleteMerchant = False
        'Else
        DeleteMerchant = True
        'End If
        ''Cmd.ActiveConnection = Nothing
        Conn.Close()
        Conn = Nothing
        Exit Function
        'ErrorHandler:
        '        Call LogErrorEvent("AdminMerchant.DeleteMerchant")

        '        If Not Conn Is Nothing Then
        '            If Conn.State <> adStateClosed Then
        '                Conn.Close()
        '            End If

        '            Conn = Nothing
        '        End If

        '        Cmd = Nothing
    End Function

    ' use the VT session vars in the db to store the
    Private Sub SaveSortField(ByVal SortField As String, ByRef SortOrder As String, Optional ByVal ToggleSortOrder As Boolean = True)


        Dim objConn As SqlConnection
        ' Dim objCmd As SqlCommand
        'Dim objRs As DataSet
        Dim strSQL As String = ""
        ' Dim StoredField As String
        'Dim StoredOrder As String
        'Dim SortOrder As String
        'Dim StoredPage As String
        Dim UpdateField As Boolean
        Dim SearchStatus As String = ""
        'Dim sqladp As SqlDataAdapter

        'objConn = CreateObject("adodb.Connection")
        'objCmd = CreateObject("ADODB.Command")
        'objRs = CreateObject("adodb.Recordset")

        'SearchStatus = GetSearchStatus
        'objConn.Open(DBConnectString)

        UpdateField = True

        ' With objCmd
        '     .Connection = objConn
        '     .CommandText = "VIAKLIX_VIRTUAL_TERMINAL.GetVTSortCriteria"
        '     .CommandType = CommandType.StoredProcedure
        '     .Parameters.Add(New SqlParameter("@sessionid", SqlDbType.VarChar, 400)).Value = SessionID
        '     .Parameters.Add(New SqlParameter("@errormsg", SqlDbType.VarChar, 50)).Direction = ParameterDirection.Output
        ' End With
        ' sqladp.SelectCommand.CommandType = CommandType.StoredProcedure
        ' sqladp.Fill(objRs, "GetVTSortCriteria
        '")
        'With objRs
        '    .CursorType = adOpenStatic
        '    .CursorLocation = adUseClient
        'End With

        'objRs = objCmd.Execute

        '------------------------------------------------------------------------
        'Returns a message to the calling ASP page.
        '------------------------------------------------------------------------
        'If Not IsNothing(objCmd.Parameters("errormsg").Value) Then
        '    'objRs = Nothing

        '    'Debug.Print("Error Msg = " & objCmd.Parameters("errormsg"))
        '    ErrMsg = objCmd.Parameters("errormsg").Value

        '    objCmd = Nothing
        '    objConn.Close()
        '    objConn = Nothing
        '    Exit Sub
        'End If
        '------------------------------------------------------------------------


        ' StoredField = SafeString(objRs.Tables(0).Rows(0).Item(0))
        'StoredOrder = SafeString(objRs.Tables(0).Rows(0).Item(1))

        'If IsNothing(objRs.Tables(0).Rows(0).Item(2)) Then
        '    StoredPage = 0
        'Else
        '    StoredPage = SafeString(objRs.Tables(0).Rows(0).Item(2))
        'End If

        'objCmd.ActiveConnection = Nothing
        'objConn.Close()

        'If ToggleSortOrder Then
        '    If UCase(StoredField) = UCase(SortField) Then
        '        UpdateField = False
        '        If CInt(StoredPage) = CInt(nPage) Then
        '            Select Case UCase(StoredOrder)
        '                Case "DESC"
        '                    SortOrder = "ASC"
        '                Case Else
        '                    SortOrder = "DESC"
        '            End Select
        '        Else
        '            SortOrder = StoredOrder
        '            SetCurrentPage()
        '        End If
        '    Else
        '        SortOrder = "ASC"
        '    End If

        'Else
        '    If SortOrder <> vbNullString Then
        '        SortOrder = StoredOrder
        '    Else
        '        SortOrder = "ASC"
        '    End If
        '    SetCurrentPage()
        'End If

        SaveSortOrder(SortOrder)

        '    If UpdateField Then
        '        If (SearchStatus) = "N" Then
        '            Select Case SortField
        '                Case "settleresponse", "settlementdatetime", "netamount", "salecount", "voidcount", "creditcount", "batch_id"
        '                Case Else
        '                    SortField = "settlementdatetime"
        '            End Select
        '        Else
        '            Select Case SortField
        '                Case "batch_id", "txn_type", "txn_date_time", "tranamount", "auth_avs_response", "cvv2response", "card_number", "txn_id"
        '                Case Else
        '                    SortField = "txn_date_time"
        '            End Select
        '        End If
        '    Else
        '        Select Case SortField
        '            Case "txn_id", "txn_type", "txn_date_time", "tranamount", "card_number"
        '            Case Else
        '                SortField = "txn_date_time"
        '        End Select
        '    End If

        'objConn.Open(DBConnectString)
        If UCase(SortField) = vbNullString Then SortField = "viaKlix_ID"

        'With objCmd
        '    .Connection = objConn
        '    .CommandText = "VIAKLIX_VIRTUAL_TERMINAL.SetVTSortField"
        '    '.Parameters.Delete(1)
        '    .Parameters.Add(New SqlParameter("@sortfield", SqlDbType.VarChar, 25)).Value = UCase(SortField)
        '    .Parameters.Add(New SqlParameter("@errormsg", SqlDbType.VarChar, 50)).Direction = ParameterDirection.Output
        'End With

        '------------------------------------------------------------------------
        'Returns a message to the calling ASP page.
        '------------------------------------------------------------------------
        'If Not IsNothing(objCmd.Parameters("errormsg").Value) Then

        '    'Debug.Print("Error Msg = " & objCmd.Parameters("errormsg"))
        '    ErrMsg = objCmd.Parameters("errormsg").Value

        '    objCmd = Nothing
        '    objConn.Close()
        '    objConn = Nothing
        '    Exit Sub
        'End If
        '------------------------------------------------------------------------

        'End With
        'objCmd.ActiveConnection = Nothing
        ' objConn.Close()
        objConn = Nothing

        Exit Sub

        'ErrorHandler:
        '        Call LogErrorEvent("AdminMerchant.SaveSortField")

        '        If Not objRs Is Nothing Then
        '            If objRs.State <> adStateClosed Then
        '                objRs.Close()
        '            End If

        '            objRs = Nothing
        '        End If

        '        If Not objConn Is Nothing Then
        '            If objConn.State <> adStateClosed Then
        '                objConn.Close()
        '            End If

        '            objConn = Nothing
        '        End If

        '        objCmd = Nothing
        '        Exit Sub
    End Sub
    Private Function GetDisplayCriteria() As DataSet
        'THIS FUNCTION RETRIEVES ALL OF THE MERCHANT'S SPECIFIED DISPLAY CRITERIA


        'Dim objConn As SqlConnection
        Dim objCmd As SqlCommand
        Dim objRs As DataSet
        'Dim strSQL As String
        'Dim sqladp As SqlDataAdapter

        'objConn = CreateObject("ADODB.Connection")
        'objCmd = CreateObject("ADODB.Command")
        'objRs = CreateObject("ADODB.Recordset")

        'objConn.Open(DBConnectString)
        objRs = New DataSet
        'With objCmd
        '    .Connection = objConn
        '    .CommandText = "VIAKLIX_VIRTUAL_TERMINAL.GetVTSession"
        '    .CommandType = CommandType.StoredProcedure
        '    .Parameters.Add(New SqlParameter("@sessionid", SqlDbType.VarChar, 40)).Value = SessionID
        '    .Parameters.Add(New SqlParameter("@errormsg", SqlDbType.VarChar, 50)).Direction = ParameterDirection.Output
        'End With

        ''With objRs
        ''    .CursorType = adOpenStatic
        ''    .CursorLocation = adUseClient
        ''End With
        'sqladp.SelectCommand.CommandType = CommandType.StoredProcedure
        'sqladp.Fill(objRs, "GetVTSession")
        'objRs = objCmd.Execute

        '------------------------------------------------------------------------
        'Returns a message to the calling ASP page.
        '------------------------------------------------------------------------
        'If Not IsNothing(objCmd.Parameters("errormsg").Value) Then

        '    'Debug.Print("Error Msg = " & objCmd.Parameters("errormsg"))
        '    ErrMsg = objCmd.Parameters("errormsg").Value

        '    objCmd = Nothing
        '    'objConn.Close()
        '    objConn = Nothing

        'End If
        '------------------------------------------------------------------------


        GetDisplayCriteria = objRs
        objCmd = Nothing

        Exit Function

        'ErrorHandler:
        '        Call LogErrorEvent("AdminMerchant.GetDisplayCriteria")
        '        'objConn.Close
        '        'Set objConn = Nothing

        '        If Not objRs Is Nothing Then
        '            If objRs.State <> adStateClosed Then
        '                objRs.Close()
        '            End If

        '            objRs = Nothing
        '        End If

        '        If Not objConn Is Nothing Then
        '            If objConn.State <> adStateClosed Then
        '                objConn.Close()
        '            End If

        '            objConn = Nothing
        '        End If

        '        objCmd = Nothing
    End Function

    Private Sub SaveCurrentPage()

        'Dim objConn As SqlConnection
        'Dim objCmd As SqlCommand

        'objConn = CreateObject("adodb.Connection")
        'objCmd = CreateObject("adodb.Command")

        'objConn.Open(DBConnectString)
        'With objCmd
        '    .Connection = objConn
        '    .CommandText = "VIAKLIX_VIRTUAL_TERMINAL.SetVTPage"
        '    .CommandType = CommandType.StoredProcedure
        '    .Parameters.Add(New SqlParameter("@sessionid", SqlDbType.VarChar, 40)).Value = SessionID
        '    .Parameters.Add(New SqlParameter("@page", SqlDbType.Int, 100)).Value = nPage
        '    .Parameters.Add(New SqlParameter("@errormsg", SqlDbType.VarChar, 50)).Direction = ParameterDirection.Output
        'End With
        'objCmd.Connection.Open()
        'objCmd.ExecuteNonQuery()

        '------------------------------------------------------------------------
        'Returns a message to the calling ASP page.
        '------------------------------------------------------------------------
        'If Not IsNothing(objCmd.Parameters("errormsg").Value) Then

        '    'Debug.Print("Error Msg = " & objCmd.Parameters("errormsg"))
        '    ErrMsg = objCmd.Parameters("errormsg").Value

        '    objCmd = Nothing
        '    objConn.Close()
        '    objConn = Nothing
        '    Exit Sub
        'End If
        '------------------------------------------------------------------------

        'End With
        'objCmd.ActiveConnection = Nothing
        'objConn.Close()
        'objConn = Nothing

        'Exit Sub

        'ErrorHandler:
        '        Call LogErrorEvent("BatchManager.SaveCurrentPage")

        '        If Not objConn Is Nothing Then
        '            If objConn.State <> adStateClosed Then
        '                objConn.Close()
        '            End If

        '            objConn = Nothing
        '        End If

        '        objCmd = Nothing
    End Sub
    Private Sub SetCurrentPage()
        Dim objRs As DataSet
        Dim StoredPage As Integer
        objRs = New DataSet
        objRs = GetDisplayCriteria()
        StoredPage = objRs.Tables(0).Rows(0).Item(7)
        'objRs.Close().objRs.tables(0).rows(0).item(7)

        SaveCurrentPage()

        Exit Sub
    End Sub
    Private Sub SaveSortOrder(ByRef Order As String)


        Dim objConn As SqlConnection
        Dim objCmd As SqlCommand
        Dim objRs As DataSet
        'Dim strSQL As String
        Dim StoredOrder As String
        Dim SortOrder As String
        Dim UpdateOrder As Boolean
        Dim BatchSearch As Boolean
        BatchSearch = False
        'Dim sqladp As SqlDataAdapter
        'objConn = CreateObject("adodb.Connection")
        'objCmd = CreateObject("adodb.Command")
        'objRs = CreateObject("adodb.Recordset")

        UpdateOrder = False
        objCmd = New SqlCommand
        objRs = New DataSet
        'objConn.Open(DBConnectString)
        'With objCmd
        '    .Connection = objConn
        '    .CommandText = "VIAKLIX_VIRTUAL_TERMINAL.GetVTSortCriteria"
        '    .CommandType = CommandType.StoredProcedure
        '    .Parameters.Add(New SqlParameter("@sessionid", SqlDbType.VarChar, 40)).Value = SessionID
        '    .Parameters.Add(New SqlParameter("@errormsg", SqlDbType.VarChar, 50)).Direction = ParameterDirection.Output
        'End With
        'sqladp.SelectCommand.CommandType = CommandType.StoredProcedure
        'sqladp.Fill(objRs, "GetVTSortCriteria")


        'objRs = objCmd.Execute

        '------------------------------------------------------------------------
        'Returns a message to the calling ASP page.
        '------------------------------------------------------------------------
        If Not IsNothing(objCmd.Parameters("errormsg").Value) Then


            ' Debug.Print("Error Msg = " & objCmd.Parameters("errormsg"))
            ErrMsg = objCmd.Parameters("errormsg").Value

            objCmd = Nothing
            '.Close()
            objConn = Nothing
            Exit Sub
        End If
        '------------------------------------------------------------------------

        StoredOrder = SafeString(objRs.Tables(0).Rows(0).Item(1))
        'objCmd.ActiveConnection = Nothing
        ' objConn.Close()

        If StoredOrder <> Order Then
            UpdateOrder = True
            SortOrder = Order
        End If

        If UpdateOrder Then
            'objConn.Open(DBConnectString)
            With objCmd
                ' .Connection = objConn
                .CommandText = "VIAKLIX_VIRTUAL_TERMINAL.SetVTSortOrder"
                '.Parameters.Delete(1)
                .Parameters.Add(New SqlParameter("@sortorder", SqlDbType.VarChar, 4)).Value = Order
                .Parameters.Add(New SqlParameter("@batch_search", SqlDbType.VarChar, 5)).Value = BatchSearch
                .Parameters.Add(New SqlParameter("@errormsg", SqlDbType.VarChar, 50)).Direction = ParameterDirection.Output
            End With

            '------------------------------------------------------------------------
            'Returns a message to the calling ASP page.
            '------------------------------------------------------------------------
            If Not IsNothing(objCmd.Parameters("errormsg").Value) Then

                ''Debug.Print("Error Msg = " & objCmd.Parameters(2"errormsg"))
                ErrMsg = objCmd.Parameters("errormsg").Value

                objCmd = Nothing
                ' objConn.Close()
                objConn = Nothing
                Exit Sub
            End If
            '------------------------------------------------------------------------

            'End With
            'objCmd.ActiveConnection = Nothing
            'objConn.Close()
            'objConn = Nothing

        End If


        'ErrorHandler:
        '        Call LogErrorEvent("BatchManager.SaveSortOrder")

        '        If Not objRs Is Nothing Then
        '            If objRs.State <> adStateClosed Then
        '                objRs.Close()
        '            End If

        '            objRs = Nothing
        '        End If

        '        If Not objConn Is Nothing Then
        '            If objConn.State <> adStateClosed Then
        '                objConn.Close()
        '            End If

        '            objConn = Nothing
        '        End If

        '        objCmd = Nothing

        '        Exit Sub
    End Sub

    Private Sub LogErrorEvent(ByVal sOrigin As String)
        Dim sErrMsg As String
        sErrMsg = ""
        sErrMsg = sErrMsg & "viaMerchAccount." & sOrigin & vbCrLf
        sErrMsg = sErrMsg & "Source: " & Err.Source & vbCrLf
        sErrMsg = sErrMsg & "Number: " & Err.Number & vbCrLf
        sErrMsg = sErrMsg & "Description: " & Err.Description
        'App.LogEvent(sErrMsg, vbLogEventTypeError)
        'Debug.Print(sErrMsg)
    End Sub


End Class
